Regex for Repeated Consonants

October 3, 2024
You said:
Please write a regex function that matches any word that has a repeated consonant in it. Examples: It should match to 'connect' because of the double n, it should not match 'banana' because the two n's are not next to each other. Only return the regex function
ChatGPT said:
ChatGPT

Here is the regex function that matches any word with a repeated consonant:

python
import re def repeated_consonant(word): return bool(re.search(r'([bcdfghjklmnpqrstvwxyz])\1', word, re.IGNORECASE))

This function checks for any repeated consonant by looking for a character from the set of consonants, followed by the same character.

You said:
That is pretty good, but this only matches the directly repeated consonants, but I need to match to the words containing these matching consonants. You can assume words are seperated by spaces.
ChatGPT said:
ChatGPT

Got it! You want to match entire words containing repeated adjacent consonants. Here's an updated version of the function that captures entire words when a pair of consecutive consonants is found:

python
import re def repeated_consonant(word): return re.findall(r'\b\w*[bcdfghjklmnpqrstvwxyz]{2,}\w*\b', word, re.IGNORECASE)

This function will return a list of words containing repeated adjacent consonants from a given string.

You said:
This is somehow matching for the string 'test' and 'bcd'
ChatGPT said:
ChatGPT

Thank you for the clarification. To address this properly, we need to ensure the regular expression strictly matches only when there are two adjacent repeated consonants in a word. Here's the corrected version:

python
import re def repeated_consonant(text): return re.findall(r'\b\w*([bcdfghjklmnpqrstvwxyz])\1\w*\b', text, re.IGNORECASE)

Explanation:

  • \b asserts a word boundary.
  • \w* allows for any word characters before or after the repeated consonants.
  • ([bcdfghjklmnpqrstvwxyz])\1 looks for a consonant followed by the same consonant (i.e., repeated adjacent consonants).
  • \b ensures we match the entire word containing repeated adjacent consonants.

This should no longer match words like "test" or "bcd" and only match words where there are adjacent repeated consonants, such as "connect".